package com.fan.shop.businesslogic;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;

import com.fan.shop.domain.StockReq;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.model.userclass.StockReqLineUserClass;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.StockReqLineService;
import com.fan.shop.services.StockReqService;
import com.fan.shop.services.StoreService;
import com.fan.shop.services.UserService;

public class StockReqUpdateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(StockReqUpdateProcess.class);

	private String requisitionNo;
	private String officerCode;
	private String storeCode;
	private String status;
	private BigDecimal postage;
	private String comment;

	private StockReqService stockReqService;
	private StockReqLineService stockReqLineService;
	private StoreService storeService;
	private UserService userService;

	private List<StockReqLineUserClass> attachedLines;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		StockReqUpdateProcess.logger = logger;
	}

	public String getRequisitionNo() {
		return requisitionNo;
	}

	public void setRequisitionNo(String requisitionNo) {
		this.requisitionNo = requisitionNo;
	}

	public String getOfficerCode() {
		return officerCode;
	}

	public void setOfficerCode(String officerCode) {
		this.officerCode = officerCode;
	}

	public String getStoreCode() {
		return storeCode;
	}

	public void setStoreCode(String storeCode) {
		this.storeCode = storeCode;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public BigDecimal getPostage() {
		return postage;
	}

	public void setPostage(BigDecimal postage) {
		this.postage = postage;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public StockReqService getStockReqService() {
		return stockReqService;
	}

	public void setStockReqService(StockReqService stockReqService) {
		this.stockReqService = stockReqService;
	}

	public StockReqLineService getStockReqLineService() {
		return stockReqLineService;
	}

	public void setStockReqLineService(StockReqLineService stockReqLineService) {
		this.stockReqLineService = stockReqLineService;
	}

	public StoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<StockReqLineUserClass> getAttachedLines() {
		return attachedLines;
	}

	public void setAttachedLines(List<StockReqLineUserClass> attachedLines) {
		this.attachedLines = attachedLines;
	}

	@Override
	public Integer processImpl() {
		if (!stockReqService.hasStockReq(requisitionNo)) {
			setErrorMessage("StockReq: " + requisitionNo + " does'nt exist!");
			return ShopGlobalVariables.FAILED;
		}
		if (!storeService.hasStore(storeCode)) {
			setErrorMessage("Store: " + storeCode + " doesn't exists!");
			return ShopGlobalVariables.FAILED;
		}
		if (!userService.hasUser(officerCode)) {
			setErrorMessage("User: " + officerCode + " doesn't exists!");
			return ShopGlobalVariables.FAILED;
		}

		StockReq stockReq = stockReqService.getStockReq(requisitionNo);
		stockReq.setOfficerCode(officerCode);
		stockReq.setStoreCode(storeCode);
		stockReq.setPostage(postage);
		stockReq.setComment(comment);

		stockReqService.updateStockReq(stockReq);

		for (StockReqLineUserClass lineUserClass : attachedLines) {
			if (stockReqLineService
					.hasStockReqLine(lineUserClass.getRequisitionNo(),
							lineUserClass.getItemCode())) {
				StockReqLineUpdateProcess stockReqLineUpdateProcess = new StockReqLineUpdateProcess();
				stockReqLineUpdateProcess.setRequisitionNo(lineUserClass
						.getRequisitionNo());
				stockReqLineUpdateProcess.setStoreCode(storeCode);
				stockReqLineUpdateProcess.setItemCode(lineUserClass
						.getItemCode());
				stockReqLineUpdateProcess.setPrice(lineUserClass.getPrice());
				stockReqLineUpdateProcess.setQty(lineUserClass.getQty());
				stockReqLineUpdateProcess.setOfficerCode(officerCode);
				stockReqLineUpdateProcess.setTransactionDate(lineUserClass
						.getTransactionDate());
				stockReqLineUpdateProcess.setLineStatus(lineUserClass
						.getLineStatus());
				stockReqLineUpdateProcess
						.setStockReqLineService(stockReqLineService);

				int result = stockReqLineUpdateProcess.process();
				if (result == ShopGlobalVariables.FAILED) {
					setErrorMessage(stockReqLineUpdateProcess.getErrorMessage());
					return ShopGlobalVariables.FAILED;
				}

			} else {
				// create a new requisition line
				StockReqLineCreateProcess stockReqLineCreateProcess = new StockReqLineCreateProcess();
				stockReqLineCreateProcess.setRequisitionNo(requisitionNo);
				stockReqLineCreateProcess.setItemCode(lineUserClass
						.getItemCode());
				stockReqLineCreateProcess.setLineStatus(lineUserClass
						.getLineStatus());
				stockReqLineCreateProcess.setOfficerCode(lineUserClass
						.getOfficerCode());
				stockReqLineCreateProcess.setPrice(lineUserClass.getPrice());
				stockReqLineCreateProcess.setQty(lineUserClass.getQty());
				stockReqLineCreateProcess
						.setStockReqLineService(stockReqLineService);
				stockReqLineCreateProcess.setStoreCode(lineUserClass
						.getStoreCode());
				stockReqLineCreateProcess.setStoreService(storeService);
				stockReqLineCreateProcess.setTransactionDate(lineUserClass
						.getTransactionDate());
				stockReqLineCreateProcess.setUserService(userService);

				int result = stockReqLineCreateProcess.process();
				if (result == ShopGlobalVariables.FAILED) {
					setErrorMessage(stockReqLineCreateProcess.getErrorMessage());
					return ShopGlobalVariables.FAILED;
				}
			}
		}

		return ShopGlobalVariables.SUCCESS;
	}

}
